#include "hplcautosamplerbasiccontroldevicepage.h"
#include "modulemanager.h"
#include "modulehplcautosampler.h"
#include "hplcsparklinkautosamplerdeviceprotocol.h"
#include "modulehplcautosamplercmdid.h"
#include "hplcsparklinkautosamplerpfcid.h"
#include <QDebug>

HplcAutoSamplerBasicControlDevicePage::HplcAutoSamplerBasicControlDevicePage(ModuleID id, QWidget *parent)
    : QWidget(parent)
    , m_moduleId(id)
{
    setWindowTitle(tr("Hplc AutoSampler Basic Control Device Page"));
    setAttribute(Qt::WA_DeleteOnClose);

    setupUI();
}

HplcAutoSamplerBasicControlDevicePage::~HplcAutoSamplerBasicControlDevicePage()
{

}

void HplcAutoSamplerBasicControlDevicePage::setupUI()
{
    ModuleHplcAutoSampler *mod =
            static_cast<ModuleHplcAutoSampler*>(moduleManager->find(m_moduleId));
    if (mod == nullptr)
        return;
    connect(mod, &ModuleHplcAutoSampler::parseCompleted,
            this, &HplcAutoSamplerBasicControlDevicePage::onParseCompleted);

    // 分析时长
    m_lbAnalysisTime = new QLabel(tr("AnalysisTime(h:m:s)"));
    m_lbAnalysisTime_colon[0] = new QLabel(":");
    m_lbAnalysisTime_colon[1] = new QLabel(":");
    m_leAnalysisTime_hour = new QLineEdit();
    m_leAnalysisTime_minute = new QLineEdit();
    m_leAnalysisTime_second = new QLineEdit();
    m_pbP_AnalysisTime = new QPushButton(tr("P"));
    connect(m_pbP_AnalysisTime, &QPushButton::clicked, this, [=](){
        QVariant var;
        hplc::Time_t time;
        time.hour = m_leAnalysisTime_hour->text().toUInt();
        time.minute = m_leAnalysisTime_minute->text().toUInt();
        time.second = m_leAnalysisTime_second->text().toUInt();
        var.setValue(time);
        mod->setProtocolCmd(hplc::sparklink::CmdID_P_AnalysisTime, var);
    });

    m_pbSP_AnalysisTime = new QPushButton(tr("SP"));
    connect(m_pbSP_AnalysisTime, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->setProtocolCmd(hplc::sparklink::CmdID_SP_AnalysisTime, var);
    });
    m_pbSA_AnalysisTime = new QPushButton(tr("SA"));
    connect(m_pbSA_AnalysisTime, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->setProtocolCmd(hplc::sparklink::CmdID_SA_AnalysisTime, var);
    });

    hblAnalysisTime = new QHBoxLayout();
    hblAnalysisTime->addWidget(m_lbAnalysisTime);
    hblAnalysisTime->addWidget(m_leAnalysisTime_hour);
    hblAnalysisTime->addWidget(m_lbAnalysisTime_colon[0]);
    hblAnalysisTime->addWidget(m_leAnalysisTime_minute);
    hblAnalysisTime->addWidget(m_lbAnalysisTime_colon[1]);
    hblAnalysisTime->addWidget(m_leAnalysisTime_second);
    hblAnalysisTime->addWidget(m_pbP_AnalysisTime);
    hblAnalysisTime->addWidget(m_pbSP_AnalysisTime);
    hblAnalysisTime->addWidget(m_pbSA_AnalysisTime);

    // loop volume
    m_lbLoopVolume = new QLabel(tr("LoopVolume(ul)"));
    m_leLoopVolume = new QLineEdit();
    m_pbP_LoopVolume = new QPushButton(tr("P"));
    connect(m_pbP_LoopVolume, &QPushButton::clicked, this, [=](){
        QVariant var;
        var.setValue(m_leLoopVolume->text().toUInt());
        mod->setProtocolCmd(hplc::sparklink::CmdID_P_LoopVolume, var);
    });

    m_pbSP_LoopVolume = new QPushButton(tr("SP"));
    connect(m_pbSP_LoopVolume, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->setProtocolCmd(hplc::sparklink::CmdID_SP_LoopVolume, var);
    });

    hblLoopVolume = new QHBoxLayout();
    hblLoopVolume->addWidget(m_lbLoopVolume);
    hblLoopVolume->addWidget(m_leLoopVolume);
    hblLoopVolume->addWidget(m_pbP_LoopVolume);
    hblLoopVolume->addWidget(m_pbSP_LoopVolume);

    //
    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->addLayout(hblAnalysisTime);
    vbl->addLayout(hblLoopVolume);
    setLayout(vbl);
}

void HplcAutoSamplerBasicControlDevicePage::onParseCompleted(int id, const QVariant &var)
{
    qDebug() << "111111111111111111111111111111111111111 device";
    switch (id)
    {
        // analysisTime
        case hplc::sparklink::CmdID_P_AnalysisTime:
        {
            hplc::Time_t time = var.value<hplc::Time_t>();
            m_leAnalysisTime_hour->setText(QString::number(time.hour));
            m_leAnalysisTime_minute->setText(QString::number(time.minute));
            m_leAnalysisTime_second->setText(QString::number(time.second));
        }
        break;

        // loopVolume
        case hplc::sparklink::CmdID_P_LoopVolume:
        {
            quint16 volume = var.toUInt();
            m_leLoopVolume->setText(QString::number(volume));
        }
        break;

#if 0
        // firstSamplePosition
        case hplc::sparklink::CmdID_P_FirstSamplePosition:
        {
            hplc::SamplePosition_t position = var.value<hplc::SamplePosition_t>();
            m_leFirstSamplePosition_PlateNumber->setText(QString::number(position.plateNumber));
            if (position.plateNumber == 3)
            {
                m_leFirstSamplePosition_RowNumber->setText(QString::number(position.vialNumber));
            }
            else
            {
                m_leFirstSamplePosition_ColumnNumber->setText(QString::number(position.columnRowNumber.columnNumber));
                m_leFirstSamplePosition_RowNumber->setText(QString::number(position.columnRowNumber.rowNumber));
            }
        }
        break;

        // lastSamplePosition
        case hplc::sparklink::CmdID_P_LastSamplePosition:
        {
            hplc::SamplePosition_t position = var.value<hplc::SamplePosition_t>();
            m_leLastSamplePosition_PlateNumber->setText(QString::number(position.plateNumber));
            if (position.plateNumber == 3)
            {
                m_leLastSamplePosition_RowNumber->setText(QString::number(position.vialNumber));
            }
            else
            {
                m_leLastSamplePosition_ColumnNumber->setText(QString::number(position.columnRowNumber.columnNumber));
                m_leLastSamplePosition_RowNumber->setText(QString::number(position.columnRowNumber.rowNumber));
            }
        }
        break;

        // flushVolume
        case hplc::sparklink::CmdID_P_FlushVolume:
        {
            quint16 volume = var.toUInt();
            getSender()->P_flushVolume(volume);
        }
        break;
        case hplc::sparklink::CmdID_SP_FlushVolume:
        {
            getSender()->SP_flushVolume();
        }
        break;

        // numberOfInjectionsOrSample
        case hplc::sparklink::CmdID_P_NumberOfInjectionsOrSample:
        {
            quint8 number = var.toUInt();
            getSender()->P_numberOfInjectionsOrSample(number);
        }
        break;
        case hplc::sparklink::CmdID_SP_NumberOfInjectionsOrSample:
        {
            getSender()->SP_numberOfInjectionsOrSample();
        }
        break;
        case hplc::sparklink::CmdID_SA_NumberOfInjectionsOrSample:
        {
            getSender()->SA_numberOfInjectionsOrSample();
        }
        break;

        // injectionMode
        case hplc::sparklink::CmdID_P_InjectionMode:
        {
            quint8 mode = var.toUInt();
            getSender()->P_injectionMode(mode);
        }
        break;
        case hplc::sparklink::CmdID_SP_InjectionMode:
        {
            getSender()->SP_injectionMode();
        }
        break;

        // syringeVolume
        case hplc::sparklink::CmdID_P_SyringeVolume:
        {
            quint16 volume = var.toUInt();
            getSender()->P_syringeVolume(volume);
        }
        break;
        case hplc::sparklink::CmdID_SP_SyringeVolume:
        {
            getSender()->SP_syringeVolume();
        }
        break;

        // tubingVolume
        case hplc::sparklink::CmdID_P_TubingVolume:
        {
            quint16 volume = var.toUInt();
            getSender()->P_tubingVolume(volume);
        }
        break;
        case hplc::sparklink::CmdID_SP_TubingVolume:
        {
            getSender()->SP_tubingVolume();
        }
        break;

        // firstTransportVial
        case hplc::sparklink::CmdID_P_FirstTransportVial:
        {
            hplc::SamplePosition_t position = var.value<hplc::SamplePosition_t>();
            getSender()->P_firstTransportVial(position.plateNumber, position.vialNumber);
        }
        break;
        case hplc::sparklink::CmdID_SP_FirstTransportVial:
        {
            getSender()->SP_firstTransportVial();
        }
        break;

        // lastTransportVial
        case hplc::sparklink::CmdID_P_LastTransportVial:
        {
            hplc::SamplePosition_t position = var.value<hplc::SamplePosition_t>();
            getSender()->P_lastTransportVial(position.plateNumber, position.vialNumber);
        }
        break;
        case hplc::sparklink::CmdID_SP_LastTransportVial:
        {
            getSender()->SP_lastTransportVial();
        }
        break;

        // sampleNeedleHeight
        case hplc::sparklink::CmdID_P_SampleNeedleHeight:
        {
            quint8 level = var.toUInt();
            getSender()->P_sampleNeedleHeight(level);
        }
        break;
        case hplc::sparklink::CmdID_SP_SampleNeedleHeight:
        {
            getSender()->SP_sampleNeedleHeight();
        }
        break;

        // syringeSpeed
        case hplc::sparklink::CmdID_P_SyringeSpeed:
        {
            quint8 level = var.toUInt();
            getSender()->P_syringeSpeed(level);
        }
        break;
        case hplc::sparklink::CmdID_SP_SyringeSpeed:
        {
            getSender()->SP_syringeSpeed();
        }
        break;

        // syringeScaleFactor
        case hplc::sparklink::CmdID_P_SyringeSpeedScaleFactor:
        {
            quint8 factor = var.toUInt();
            getSender()->P_syringeSpeedScaleFactor(factor);
        }
        break;
        case hplc::sparklink::CmdID_SP_SyringeSpeedScaleFactor:
        {
            getSender()->SP_syringeSpeedScaleFactor();
        }
        break;

        // bufferVolume
        case hplc::sparklink::CmdID_P_BufferVolume:
        {
            quint16 volume = var.toUInt();
            getSender()->P_bufferVolume(volume);
        }
        break;
        case hplc::sparklink::CmdID_SP_BufferVolume:
        {
            getSender()->SP_bufferVolume();
        }
        break;

        // actualSampleNumber
        case hplc::sparklink::CmdID_SA_ActualSampleNumber:
        {
            getSender()->SA_actualSampleNumber();
        }
        break;

        // status
        case hplc::sparklink::CmdID_SA_Status:
        {
            getSender()->SA_status();
        }
        break;

        // errorCode
        case hplc::sparklink::CmdID_SA_ErrorCode:
        {
            getSender()->SA_errorCode();
        }
        break;

        // resetErrorCode
        case hplc::sparklink::CmdID_C_ResetErrorCode:
        {
            quint8 flag = var.toUInt();
            getSender()->C_resetErrorCode(flag);
        }
        break;

        // airSegment
        case hplc::sparklink::CmdID_P_AirSegment:
        {
            quint8 isUsed = var.toUInt();
            getSender()->P_airSegment(isUsed);
        }
        break;
        case hplc::sparklink::CmdID_SP_AirSegment:
        {
            getSender()->SP_airSegment();
        }
        break;

        // skipMissingSamplePosition
        case hplc::sparklink::CmdID_P_SkipMissingSamplePosition:
        {
            quint8 flag = var.toUInt();
            getSender()->P_skipMissingSamplePosition(flag);
        }
        break;
        case hplc::sparklink::CmdID_SP_SkipMissingSamplePosition:
        {
            getSender()->SP_skipMissingSamplePosition();
        }
        break;

        // headSpacePressure
        case hplc::sparklink::CmdID_P_HeadSpacePressure:
        {
            quint8 flag = var.toUInt();
            getSender()->P_headSpacePressure(flag);
        }
        break;
        case hplc::sparklink::CmdID_SP_HeadSpacePressure:
        {
            getSender()->SP_headSpacePressure();
        }
        break;

        // resetOutputAfterSerials
        case hplc::sparklink::CmdID_P_ResetOutputAfterSerials:
        {
            quint8 flag = var.toUInt();
            getSender()->P_resetOutputAfterSerials(flag);
        }
        break;
        case hplc::sparklink::CmdID_SP_ResetOutputAfterSerials:
        {
            getSender()->SP_resetOutputAfterSerials();
        }
        break;

        // usePrepMode
        case hplc::sparklink::CmdID_P_UsePrepMode:
        {
            quint8 flag = var.toUInt();
            getSender()->P_usePrepMode(flag);
        }
        break;
        case hplc::sparklink::CmdID_SP_UsePrepMode:
        {
            getSender()->SP_usePrepMode();
        }
        break;

        // traySegmentSettings
        case hplc::sparklink::CmdID_P_TraySegmentSettings:
        {
            hplc::TraySegmentSetting_t setting = var.value<hplc::TraySegmentSetting_t>();

            if (setting.leftTrayType == 3 && setting.rightTrayType == 3)
            {
                getSender()->P_traySegmentSettings_SinglePlate(setting.singlePlateType);
            }
            else
            {
                getSender()->P_traySegmentSettings(setting.leftTrayType,
                                                               setting.rightTrayType,
                                                               setting.separatePlateType.leftType,
                                                               setting.separatePlateType.rightType);
            }
        }
        break;
        case hplc::sparklink::CmdID_SP_TraySegmentSettings:
        {
            getSender()->SP_traySegmentSettings();
        }
        break;

        // syncCondition
        case hplc::sparklink::CmdID_P_SyncCondition:
        {
            hplc::SyncCondition_t condition = var.value<hplc::SyncCondition_t>();
            getSender()->P_syncCondition(condition.isSyncWithNextInjection,
                                                     condition.isSyncWithLoad,
                                                     condition.isSyncWithInject,
                                                     condition.isSyncWithValveWash);
        }
        break;
        case hplc::sparklink::CmdID_SP_SyncCondition:
        {
            getSender()->SP_syncCondition();
        }
        break;

        // injectionVolume
        case hplc::sparklink::CmdID_P_InjectionVolume:
        {
            quint16 volume = var.toUInt();
            getSender()->P_injectionVolume(volume);
        }
        break;
        case hplc::sparklink::CmdID_SP_InjectionVolume:
        {
            getSender()->SP_injectionVolume();
        }
        break;

        // washBetween
        case hplc::sparklink::CmdID_P_WashBetween:
        {
            quint8 between = var.toUInt();
            getSender()->P_washBetween(between);
        }
        break;
        case hplc::sparklink::CmdID_SP_WashBetween:
        {
            getSender()->SP_washBetween();
        }
        break;

        // washTimes
        case hplc::sparklink::CmdID_P_WashTimes:
        {
            hplc::WashTimesEx_t times = var.value<hplc::WashTimesEx_t>();
            getSender()->P_washTimes(times.steps, times.times);
        }
        break;
        case hplc::sparklink::CmdID_SP_WashTimes:
        {
            quint8 steps = var.toUInt();
            getSender()->SP_washTimes(steps);
        }
        break;

        // washSyringeValveSolventPort
        case hplc::sparklink::CmdID_P_WashSyringeValveSolventPort:
        {
            hplc::WashSyringeValveSolventPortEx_t port = var.value<hplc::WashSyringeValveSolventPortEx_t>();
            getSender()->P_washSyringeValveSolventPort(port.steps, port.port);
        }
        break;
        case hplc::sparklink::CmdID_SP_WashSyringeValveSolventPort:
        {
            quint8 steps = var.toUInt();
            getSender()->SP_washSyringeValveSolventPort(steps);
        }
        break;

        // fillTransportPositionTimes
        case hplc::sparklink::CmdID_P_FillTransportPositionTimes:
        {
            hplc::FillTransportPositionTimes_t times = var.value<hplc::FillTransportPositionTimes_t>();
            getSender()->P_fillTransportPositionTimes(times.port, times.port);
        }
        break;
        case hplc::sparklink::CmdID_SP_FillTransportPositionTimes:
        {
            getSender()->SP_fillTransportPositionTimes();
        }
        break;

        // washVolume
        case hplc::sparklink::CmdID_P_WashVolume:
        {
            hplc::WashVolumeEx_t volume = var.value<hplc::WashVolumeEx_t>();
            getSender()->P_washVolume(volume.steps, volume.volume);
        }
        break;
        case hplc::sparklink::CmdID_SP_WashVolume:
        {
            quint8 steps = var.toUInt();
            getSender()->SP_washVolume(steps);
        }
        break;

        // useInMethod
        case hplc::sparklink::CmdID_P_UseInMethod:
        {
            hplc::UseInMethod_t isUseInMethod = var.value<hplc::UseInMethod_t>();
            getSender()->P_useInMethod(isUseInMethod.isUseTimedEvents,
                                                   isUseInMethod.isUseMix);
        }
        break;
        case hplc::sparklink::CmdID_SP_UseInMethod:
        {
            getSender()->SP_useInMethod();
        }
        break;

        // serialNumber
        case hplc::sparklink::CmdID_P_SerialNumber:
        {
            quint32 serial = var.toUInt();
            getSender()->P_serialNumber(serial);
        }
        break;
        case hplc::sparklink::CmdID_SP_SerialNumber:
        {
            getSender()->SP_serialNumber();
        }
        break;

        // clearMixOrUserProg
        case hplc::sparklink::CmdID_P_ClearMixOrUserProg:
        {
            quint8 flag = var.toUInt();
            getSender()->P_clearMixOrUserProg(flag);
        }
        break;

        // clearMethod
        case hplc::sparklink::CmdID_P_ClearMethod:
        {
            quint8 flag = var.toUInt();
            getSender()->P_clearMethod(flag);
        }
        break;

        // startOrStop
        case hplc::sparklink::CmdID_C_StartOrStop:
        {
            hplc::StartOrStopEx_t startOrStop = var.value<hplc::StartOrStopEx_t>();
            if (startOrStop.ai == 1)
            {
                getSender()->C_startOrStop_01(startOrStop.startUserProg,
                                                          startOrStop.startOrStop);
            }
            else
            {
                getSender()->C_startOrStop_02();
            }
        }
        break;

        // startOrStop
        case hplc::sparklink::CmdID_C_HoldOrContinue:
        {
            quint8 flag = var.toUInt();
            getSender()->C_holdOrContinue(flag);
        }
        break;

        // searchSamplePosition
        case hplc::sparklink::CmdID_C_SearchSamplePosition:
        {
            hplc::SearchSamplePosition_t position = var.value<hplc::SearchSamplePosition_t>();
            getSender()->C_searchSamplePosition(position.plateNumber,
                                                            position.platePosition);
        }
        break;

        // searchSamplePosition
        case hplc::sparklink::CmdID_C_InitialWash:
        {
            hplc::InitialWash_t initialWash = var.value<hplc::InitialWash_t>();
            getSender()->C_initialWash(initialWash.port,
                                                   initialWash.startInitialWash);
        }
        break;

        // moveSyringe
        case hplc::sparklink::CmdID_C_MoveSyringe:
        {
            quint8 moveSyringe = var.toUInt();
            getSender()->C_moveSyringe(moveSyringe);
        }
        break;

        // doorSensor
        case hplc::sparklink::CmdID_SA_DoorSensor:
        {
            getSender()->SA_doorSensor();
        }
        break;
#endif
        default:
        break;
    }
}

void HplcAutoSamplerBasicControlDevicePage::closeEvent(QCloseEvent *event)
{
    ModuleHplcAutoSampler *mod =
            static_cast<ModuleHplcAutoSampler*>(moduleManager->find(m_moduleId));
    if (mod)
        mod->disconnect(this);
    event->accept();
}
